package com.fzshang.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.commen.bean.VarietyClassification;
import com.fzshang.commen.bean.layuiBean.ResultPage;
import com.fzshang.commen.bean.layuiBean.VoPage;
import com.fzshang.commen.controller.BaseController;
import com.fzshang.commen.entity.ApiResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 菜品分类 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-02-24
 */
@Controller
@RequestMapping("admin/varietyClassification")
public class VarietyClassificationController extends BaseController {

    /**
     * @param model
     * @return 主页面
     */
    @RequestMapping(value = "varietyClassification.html")
    public String varietyClassification(Model model){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",2);
        model.addAttribute("user",userService.list(queryWrapper));
        model.addAttribute("userId",getUser().getId());
        return "admin/varietyClassification/varietyClassification";
    }

    /**
     * @return 添加页面
     */
    @RequestMapping(value = "varietyClassification_add.html")
    public String varietyClassification_add(){
        return "admin/varietyClassification/varietyClassification_add";
    }

    /**
     * @param model
     * @param id
     * @return 编辑
     */
    @RequestMapping(value = "varietyClassification_edit.html")
    public String varietyClassification_edit(Model model,Long id){
        model.addAttribute("varietyClassification",varietyClassificationService.getById(id));
        return "admin/varietyClassification/varietyClassification_edit";
    }

    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<VarietyClassification> selectMyPage(VoPage voPage, VarietyClassification varietyClassification){
        return varietyClassificationService.selectMyPage(voPage,varietyClassification,getUser().getId());
    }


    /**
     * @param varietyClassification
     * @return 添加和修改
     */
    @RequestMapping(value = "saveOrUpdateById")
    @ResponseBody
    public ApiResult saveOrUpdateById(@RequestBody VarietyClassification varietyClassification){
        varietyClassification.setCreateTime(System.currentTimeMillis());
        varietyClassification.setUserId(getUser().getId());
        if(varietyClassificationService.saveOrUpdate(varietyClassification)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    /**
     * @param id
     * @return 删除
     */
    @RequestMapping(value = "deleteById")
    @ResponseBody
    public ApiResult deleteById(Long id){
        if(varietyClassificationService.removeById(id)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

}
